const path = require('path');
const fs = require('fs');

module.exports = (message, env) => {
    if(message.request.status){
        return message;
    }

    if(message.request.method !== 'DELETE'){
        return message;
    }

    if(message.request.path.indexOf('.') === 0){
        message.response.status = 403;
        return message;
    }

    const targetPath = path.resolve(env.root + message.request.path);
    if(!fs.existsSync(targetPath)){
        message.response.status = 404;
        return message;
    }

    const requestPathStat = fs.statSync(targetPath);
    if(requestPathStat.isDirectory()){
        message.response.status = 403;
        return message;
    }

    fs.unlinkSync(targetPath);
    message.response.status = 200;
    return message;
}